package com.bos.web;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author misterWei
 * @create 2018年11月14号:11点28分
 * @mailbox mynameisweiyan@gmail.com
 */
//优化代码的构建
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

    public BaseAction() {
       //使用反射优化获取子类泛型,实现model实例化
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
        try {
            model= modelClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    protected T model;
    @Override
    public T getModel() {
        return model;
    }

    protected Integer page;
    protected Integer rows;

    public void setPage(Integer page) {
        this.page = page;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }
}
